package light.head.controller.site;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.site.CommodityDAO;
import light.head.dao.site.ParkCommodityDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.ParkCommodity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 *	园区产品展示
 */
@Controller
@RequestMapping(Url.PARKCOMMODITY)
public class ParkCommodityController extends SupporterController{
	private ParkCommodityDAO pcDAO;
	private CommodityDAO commDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PARKCOMMODITY_INIT);
	}

	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.SORT, Parm.DELETED, Parm.STATUS);
		if (pcDAO.findByMap(map, pb) == RESULT.SUCCESS){
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}		
		return new ModelAndView(Url.PARKCOMMODITY_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (null != code) {
			ParkCommodity pc = pcDAO.get(code);
			request.setAttribute(Parm.PARKCOMMODITY, pc);
		}
		return new ModelAndView(Url.PARKCOMMODITY_DEIT);
	}
	
	@RequestMapping(Url.BATCHEDIT)
	public ModelAndView batchEdit( HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PARKBATCH_EDIT);
	}
	//单个添加
	@RequestMapping(value = Url.SAVE)
	public void save(ParkCommodity pc,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (pc.getCode() == null) {
			pc.setDeleted(false);
			pc.setCu(cu_code(request));
			pc.setCt(new Date());
			result = pcDAO.add(pc);
		} else {
			result = pcDAO.upd(pc);
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	//批量添加
	@RequestMapping(value = Url.SAVEBATCH)
	public void saveBatch(ParkCommodity pc,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(pc!=null){
			for(int i = 0; i<pc.getList().size();i++){
				if(pc.getList().get(i).getName()!=null){
					pc.getList().get(i).setDeleted(false);
					pc.getList().get(i).setCu(cu_code(request));
					pc.getList().get(i).setCt(new Date());
					pc.getList().get(i).setStatus(Byte.parseByte(Sys.S_ONE));
					result = pcDAO.add(pc.getList().get(i));
				}
			}
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	//企业申请商品首页展示		
	@RequestMapping(value = Url.APPLYINDEXSHOW)
	public void applyIndexShow(Integer code,Integer oc,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code!=null && oc !=null){
			ParkCommodity pc = new ParkCommodity();
			pc.setDeleted(false);
			pc.setCu(cu_code(request));
			pc.setCt(new Date());
			pc.setCc(code);
			pc.setOc(oc);
			pc.setStatus(Byte.parseByte(Sys.S_ZERO));
			result = pcDAO.add(pc);
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code!=null){
			ParkCommodity pc = pcDAO.get(code);
			pc.setDeleted(true);
			result=pcDAO.upd(pc);
		}
		if(result==RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	
	//单个辅助查询
	@RequestMapping(Url.PARKQUERYCOMMODITYINIT)
	public ModelAndView queryCommodityInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PARKQUERYCOMMODITY_INIT);
	}
	@RequestMapping(value = Url.PARKQUERYCOMMODITYLIST)
	public ModelAndView queryCommodityList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		if (commDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.PARKQUERYCOMMODITY_LIST);
	}
	//批量辅助查询
	@RequestMapping(Url.PARKBATCHCOMMODITYINIT)
	public ModelAndView batchCommodityInit(Integer index, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute("index", index);
		return new ModelAndView(Url.PARKBATCHCOMMODITY_INIT);
	}
	@RequestMapping(value = Url.PARKBATCHCOMMODITYLIST)
	public ModelAndView batchCommodityList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		if (commDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.PARKBATCHCOMMODITY_LIST);
	}
	
	@Autowired
	public void setCommDAO(CommodityDAO commDAO) {
		this.commDAO = commDAO;
	}
	@Autowired
	public void setPcDAO(ParkCommodityDAO pcDAO) {
		this.pcDAO = pcDAO;
	}
}
